package com.ssm.studen.controller;

import com.ssm.studen.model.Answer;
import com.ssm.studen.service.AnswerService;
import com.ssm.studen.tool.ResponseMessage;
import com.ssm.studen.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 答题表：
 *      对外提供数据服务接口
 */
@Api(tags = "answer")
@RestController
public class AnswerController {

    @Autowired
    private AnswerService answerService;


    /************************试卷添加方法****************************/
    @ApiOperation("试卷添加方法")
    @RequestMapping(value = "/addAnswer",method = RequestMethod.GET)
    public int addAnswer(Answer answer){
        int IsAdd = 0;
        //传入数据判断
        //1.实体对象不能为空
        if(answer != null){
            //2.不能为空
            if(answer.getEpnum()!=0&&
                    answer.getSnum()!=0&&
                    answer.getAhg()!=null&&
                    answer.getAstart()!=null&&
                    answer.getAend()!=null&&
                    answer.getAdecstart()!=null&&
                    answer.getAissuetime()!=null&&
                    answer.getTnum()!=0&&
                    answer.getAissue()!=null&&
                    answer.getAsubmit()!=null&&
                    answer.getAsubtime()!=null&&
                    answer.getAtotalpoint()!=0){
                //3.添加试卷数据
                IsAdd = answerService.addAnswer(answer);
            }
        }
        return IsAdd;
    }


    /*************************试卷更新方法****************************/
    @ApiOperation("试卷更新方法")
    @RequestMapping(value = "/updateAnswer",method = RequestMethod.GET)
    public ResponseMessage updateRole(Answer answer){
        //传入的参数不能为空
        if(answer != null){
            //参数当中主键不能为0
            if(answer.getAnum() != 0){
                if(answerService.updateAnswer(answer) == 1){
                    if(answer.getEpnum()!=0&&
                            answer.getSnum()!=0&&
                            answer.getAhg()!=null&&
                            answer.getAstart()!=null&&
                            answer.getAend()!=null&&
                            answer.getAdecstart()!=null&&
                            answer.getAissuetime()!=null&&
                            answer.getTnum()!=0&&
                            answer.getAissue()!=null&&
                            answer.getAsubmit()!=null&&
                            answer.getAsubtime()!=null&&
                            answer.getAtotalpoint()!=0){
                        answerService.updateAnswer(answer);
                    }
                    return Result.success("更新成功");
                }else{
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入参数不正确");
    }


    /*************************试卷查询方法****************************/
    @ApiOperation("试卷查询方法")
    @RequestMapping(value = "/selectAnswer",method = RequestMethod.PUT)
    public ResponseMessage selectAnswer(int anum){
        Map<String,List> map = new HashMap<>();
        List<Answer> listAnswer = answerService.selectAnswer(anum);
        map.put("list",listAnswer);
        return Result.success("200",map);
    }
}

























